using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Awaiter;
using Canvas.ImageRotate;
using Nsb.Classes;
using Theme;
using WhirliGig.Classes;
using Yoshi.Classes;
using YoshiBase.Classes;
using jQueryApi;

namespace Yoshi.Clusters
{
    [IgnoreNamespace]
    public sealed class TopBar : Cluster
    {
        public static TopBar Instance;
        private readonly Dictionary _options;
        private jQueryObject _barEl;
        private bool _linksHidden;
        private static GigShowEdit _gigShow;

        public TopBar(jQueryObject element, Dictionary options)
        {
            Instance = this;
            Element = element;
            _options = options ?? new Dictionary("links", true);

            Build();
        }

        private void Build()
        {
            _barEl = jQuery.FromHtml("<div id='TopBar' class='AboveContent'/>")
                .PrependTo(Element);

            if ((bool)_options["links"])
            {
                NewLink(TopBarTheme.HomeBtnText, AllPages.HomePage)
                    .AppendTo(_barEl)
                    .AddClass("BarLink")
                    ;

                NewLink(TopBarTheme.MagneticListBtnText, AllPages.MagneticListPage)
                    .AppendTo(_barEl)
                    .AddClass("BarLink")
                    ;

                NewLink(TopBarTheme.SearchBtnText, AllPages.SearchPage)
                    .AppendTo(_barEl)
                    .AddClass("BarLink")
                    ;

                NewLink(TopBarTheme.EmailBtnText, AllPages.EmailPage)
                    .AppendTo(_barEl)
                    .AddClass("BarLink")
                    ;

                NewLink(TopBarTheme.GamesBtnText, AllPages.GamesPage)
                    .AppendTo(_barEl)
                    .AddClass("BarLink")
                    ;

                NewLink(TopBarTheme.FeaturesBtnText, AllPages.FeaturesPage)
                    .AppendTo(_barEl)
                    .AddClass("BarLink")
                    ;

                List<string> gotGear = new List<string>();
                gotGear.Add(AllPages.HomePage);
                gotGear.Add(AllPages.MagneticListPage);
                gotGear.Add(AllPages.SearchPage);
                gotGear.Add(AllPages.EmailPage);
                gotGear.Add(AllPages.GamesPage);
                gotGear.Add(AllPages.FeaturesPage);

                if (gotGear.Contains(CurrentPage.PageName))
                    AddSlides();

                jQuery.FromHtml("<span id='SavedIndicator'/>")
                    .AppendTo(_barEl)
                    ;
            }
        }

        private void AddSlides()
        {
            jQueryObject slideControls = jQuery.FromHtml("<div class='SlideControls'/>").AppendTo(_barEl);

            new Await()
                .WaitDx(delegate { return Master.Profile.MyId != null; })
                .LoadText("ColorPickerTable", "Scripts/Data/GigColorPicker.html")
                .LoadScript("Scripts/WhirliGig.debug.js",
                            delegate(Await aw)
                            {
                                if (_gigShow != null)
                                    _gigShow.Destroy();
                                _gigShow = new GigShowEdit();
                                _gigShow.Editing = true;
                                _gigShow.InitializeAw(
                                    aw,
                                    new Dictionary(
                                        "PartitionKey", "db/gigs/" + CurrentPage.PageName,
                                        "RowKey", Master.Profile.MyId,
                                        "PageSelectTo", "#PageGigs",
                                        "SlideControls", slideControls
                                        )
                                    );
                            }
                )
                .AddDl(delegate(Await aw) { _gigShow.RestoreAw(aw); })
                .Commit();
        }

        public void HideLinksAw(Await awp)
        {
            _linksHidden = true;
            new Await()
                .WaitDx(delegate { return _barEl.Find(".CornerUser").Length > 0; })
                .AddDx(
                    delegate
                    {
                        _barEl.Find(".CornerUser")
                            .Unbind("click")
                            .RemoveClass("Hoverable")
                            ;
                        _barEl.Find("a")
                            .CSS("visibility", "hidden")
                            ;
                    })
                .Commit(awp);
        }

        public void ShowUser()
        {
            new Await()
                .AddDx(delegate { jQuery.Select("#TopBar .CornerUser").Remove(); })
                .AddDx(
                    delegate
                    {
                        string name = Master.Profile.My != null ? Script.Value(Master.Profile.My.alias, Master.Profile.My.name, "") : "";
                        jQuery.FromHtml("<div class='CornerUser Hoverable'/>")
                            .AppendTo(_barEl)
                            .Append(jQuery.FromHtml("<span class='UserName'>" + name + "</span>"))
                            .Append(Master.FbUser.PhotoTag.AddClass("UserPhoto AboveContent"))
                            //.Append(jQuery.FromHtml("<span class='ProfileVis'>" + TopBarTheme.ProfileVis + "</span>"))
                            .Click(delegate { Master.GoPage(AllPages.ProfilePage); })
                            .Plugin<IRot>().Rotate(new Dictionary("animateTo", 360, "duration", 700))
                            ;

                        if (!(bool)_options["links"])
                        {
                            _barEl.Find(".CornerUser")
                                .Unbind("click")
                                .RemoveClass("Hoverable")
                                ;
                        }
                    }
                )
                .AddDx(delegate(Await aw) { if (_linksHidden) HideLinksAw(aw); })
                .Commit()
                ;
        }

        public void UpdatePage(PageUpdate pu)
        {
            jQuery.Select("#SavedIndicator").Html(pu.Saved ? TopBarTheme.Saved : TopBarTheme.Unsaved);
        }

        public static jQueryObject NewLink(string face, string page)
        {
            jQueryObject lnk = jQuery.FromHtml("<a/>")
                .Attribute("href", "javascript:;")
                .Html(face)
                .Plugin<Jewl>().SwallowClick(delegate { Master.GoPage(page); })
                ;

            if (Type.GetType(page) == Master.Page.GetType())
                lnk.AddClass("CurrentPage");
            return lnk;
        }
    }
}